<?php

class Session {

	// Protected Session Names
	
	// AccessoryPrice - SH 25/03/09
	// SIMFreePrice - SH 25/03/09
	// URL - SH 25/03/09
	// ShopCode - SH 25/03/09
	// Affiliate - SH 25/03/09
	// esid - SH 25/03/09
	// Environment - SH 19/05/09
	// Checkout - SH 20/05/09
	// BranchCode - SH 21/05/09
	
	// Protected Cookies
	
	// __session - SH 25/03/09

	public function __construct($site, $environment) {
		// set all variables needed in a seesion which needs starting if it doesn't exist
		if(!isset($_COOKIE['__session'])) {
			session_start();
			setcookie("__session", session_id(), time() + 3600);
			$_SESSION['esid'] = session_id();
		} else {
			$_SESSION['esid'] = $_COOKIE['__session'];
		}
		// set all session data
		$_SESSION['AccessoryPrice']	= SiteDefaults::AccessoryPrice($site);
		$_SESSION['SIMFreePrice']	= SiteDefaults::SIMFreePrice($site);
		$_SESSION['URL']			= SiteDefaults::URL($site, $environment);
		$_SESSION['Environment']	= $environment;
		$_SESSION['ShopCode']		= $site;
		$_SESSION['esid']			= $_SESSION['esid'];
		// get affilaite information
		if(strrpos($_SERVER["REQUEST_URI"], "source=")) {
			// there is an instance
			$position	= strrpos($_SERVER["REQUEST_URI"], "source=");
			$string		= substr($_SERVER["REQUEST_URI"], $position);
			
			// there could be more data, so strip it out		
			if(strpos($string, "&")) {
				$position 	=  strpos($string, "&");
				$string		= substr($string, 0, $position);
			}
			
			// remove the source= leaving only affiliate code
			$position	= strpos($string, "=");
			$final		= substr($string, $position);
			$final		= substr($final, 1);
			
			// set request to pick up source code
			$_SESSION['Affiliate'] = $final;
		}
		// set basket limitations
		$basketlimits				= SiteDefaults::BasketLimitations($site);
		$_SESSION['Checkout']		= true;
		$_SESSION['BranchCode']		= SiteDefaults::BranchCode($site);
	}
}
?>